package model;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UserModel {
	Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    
    UserModel(){
   	 try {
	            Class.forName("oracle.jdbc.driver.OracleDriver");
	            con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521","gringotts","oksana");
	            
   		/* Class.forName("org.h2.Driver");
         con = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
         */
	        
	        } catch (ClassNotFoundException e) {
	            e.printStackTrace();
	        } catch (SQLException e) {
	            e.printStackTrace();
	        }
    }
    
    int getBalancle(BigInteger idCard, int pin) throws SQLException{
    	stmt = con.createStatement();
        rs = stmt.executeQuery("SELECT BALANCE FROM ACOUNT WHERE USERNO ="+idCard+" AND PIN ="+pin);
        rs.next();
        return rs.getInt(1);

    }
    private int bal(BigInteger idCard) throws SQLException{
    	stmt = con.createStatement();
        rs = stmt.executeQuery("SELECT BALANCE FROM ACOUNT WHERE USERNO ="+idCard);
        rs.next();
        return rs.getInt(1);

    }
    public void putMoneyToAcount(BigInteger idCard,int amount) throws SQLException{
    	if(checkForCardId(idCard)){
    		 setBalance(idCard,bal(idCard)+amount);
    	}
    		
    		
    }
    
   public boolean checkAcount(BigInteger idCard, int pin) throws SQLException{
    	stmt = con.createStatement();
        rs = stmt.executeQuery("SELECT BALANCE FROM ACOUNT WHERE USERNO ="+idCard+" AND PIN ="+pin);
        if(rs.next())
            return true;
            else return false;

    }
    boolean checkForCardId(BigInteger idCard) throws SQLException{
    	stmt = con.createStatement();
        rs = stmt.executeQuery("SELECT * FROM ACOUNT WHERE USERNO ="+idCard);
        
        if(rs.next())
        return true;
        else return false;
    }
    
    void setBalance(BigInteger idCard,int balance) throws SQLException
    {
    	stmt = con.createStatement();
        rs = stmt.executeQuery("UPDATE ACOUNT SET BALANCE= "+balance+" WHERE USERNO ="+idCard);
    }
    
 
    public static void main(String[] args){
    
    	UserModel model= new UserModel();
    	try {
			System.out.println(model.getBalancle(new BigInteger("1111222233334444"), 1234));
			System.out.println(model.checkForCardId(new BigInteger("1111222233334444")));
			System.out.println(model.checkAcount(new BigInteger("1111222233334444"), 1234));
			model.setBalance(new BigInteger("1111222233334444"), 1000);
			System.out.println(model.getBalancle(new BigInteger("1111222233334444"), 1234));
	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    
    }
}
